package com.rabbiter.bms.web;
import com.rabbiter.bms.model.*;
import com.rabbiter.bms.service.TscaleService;
import com.rabbiter.bms.utils.MyResult;
import com.rabbiter.bms.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/tscale")
public class TscaleController {
    @Autowired
    TscaleService tscaleService;

    // 获取图书数量
    @GetMapping(value = "/getCount")
    public Integer getCount(){
        return tscaleService.getCount();
    }

    // 查询所有图书信息
    @GetMapping(value = "/queryBookInfos")
    public List<BookInfo> queryBookInfos(){
        return tscaleService.queryBookInfos();
    }

    // 分页搜索查询图书信息 params: {page, limit, bookname, bookauthor, booktypeid}
    @GetMapping(value = "/queryBookInfosByPage")
    public Map<String, Object> queryBookInfosByPage(@RequestParam Map<String, Object> params){
        MyUtils.parsePageParams(params);
        int count = tscaleService.getSearchCount(params);  // 获得总数
        List<BookInfo> bookInfos = tscaleService.searchBookInfosByPage(params);  // 分页查询
//        System.out.println("controller运行成功");
        return MyResult.getListResultMap(0, "success", count, bookInfos);
    }

    // 添加图书信息
    @PostMapping(value = "/addBookInfo")
    public Integer addBookInfo(@RequestBody BookInfo bookInfo){
        return tscaleService.addBookInfo(bookInfo);
    }

    //测试
    @GetMapping(value = "/getSearchCount")
    public int getSearchCount(@RequestParam Map<String, Object> params){
//        System.out.println("测试controller运行");
        MyUtils.parsePageParams(params);
        return tscaleService.getSearchCount(params);
    }

    // 删除图书信息
    @DeleteMapping(value = "/deleteBookInfo")
    public Integer deleteBookInfo(@RequestBody BookInfo bookInfo){
        return tscaleService.deleteBookInfo(bookInfo);
    }

    // 删除一些图书信息
    @DeleteMapping(value = "/deleteBookInfos")
    public Integer deleteBookInfos(@RequestBody List<BookInfo> bookInfos){
        return tscaleService.deleteBookInfos(bookInfos);
    }

    // 更新图书信息
    @PutMapping(value = "/updateBookInfo")
    public Integer updateBookInfo(@RequestBody BookInfo bookInfo){
        return tscaleService.updateBookInfo(bookInfo);
    }

    public Integer SearchCount(@RequestParam Map<String, Object> params){
        MyUtils.parsePageParams(params);
        return tscaleService.SearchCount(params);
    }

    public int SearchUploadCount(@RequestBody UploadTable table){
        return tscaleService.SearchUploadCount(table);
    }

    // 分页搜索查询图书信息 params: {page, limit, bh, name, nameN,userId}
    @GetMapping(value = "/querytscalesByPage")
    public Map<String, Object> queryTScalesByPage (@RequestParam Map<String, Object> params){
//        MyUtils.parsePageParams(params);
        int count = tscaleService.SearchCount(params);  // 获得总数
//        System.out.println("SearchCount执行成功");
        List<TScaleTypeTree> tScaleTypeTrees = tscaleService.searchTScalesByPage(params);  // 分页查询
//        System.out.println("controller运行成功");
        return MyResult.getListResultMap(0, "success", count, tScaleTypeTrees);
    }

    @PostMapping(value = "/insertUploadTable")
    public Integer insertUploadTable(@RequestBody List<TScaleVO> TScales,@RequestParam("userId" ) int userId){
//        System.out.println("接受数据：");
//        System.out.println("TScales:" +TScales);
//        System.out.println("userId:"+userId);
//        return 0;
        return tscaleService.insertUploadTable(TScales,userId);
    }

    @PostMapping(value = "/deleteUploadTable")
    public Integer deleteUploadTable(@RequestBody List<TScaleVO> TScales,@RequestParam("userId") int userId){
//        System.out.println("接受数据：");
//        System.out.println("TScales:" +TScales);
//        System.out.println("userId:"+userId);
//        return 0;
//            System.out.println(TScales);
//            return 0;
        return tscaleService.deleteUploadTable(TScales,userId);
    }
    //测试接受情况
    @PostMapping(value = "/uploadRequestParam")
    public int uploadRequestParam (@RequestBody AnswerSheet answerSheet){
//        MyUtils.parsePageParams(params);
//        int count = tscaleService.SearchCount(params);  // 获得总数
//        System.out.println("SearchCount执行成功");
//        List<TScaleDTO> TScales = tscaleService.searchTScalesByPage(params);  // 分页查询
//        System.out.println("controller运行成功");
//        return MyResult.getListResultMap(0, "success", count, TScales);
        Map<String,Object> params = answerSheet.getParams();
        System.out.println(params);
        for(String key : params.keySet()){
            System.out.println(key+":"+params.get(key));
        }
//        System.out.println(answerSheet);
        return 0;
    }
    @PostMapping(value = "/selectQuestionnaireByFormId")
    public Map<String, Object> selectQuestionnaireByFormId(@RequestBody AnswerSheet answerSheet){
//        MyUtils.parsePageParams(params);
//        System.out.println(answerSheet.getFormId());
        int count = tscaleService.getCountQuestionnaire(answerSheet);  // 获得总数
        List<Questionnaire> questionnaires = tscaleService.selectQuestionnaireByFormId(answerSheet);  // 分页查询
//        System.out.println("controller运行成功");
        return MyResult.getListResultMap(0, "success", count, questionnaires);
    }
    @PostMapping(value = "/insertResult")
    public int insertResult(@RequestBody AnswerSheet answerSheet){
//        System.out.println("接受数据：");
//        System.out.println("TScales:" +TScales);
//        System.out.println("userId:"+userId);
//        return 0;
        return tscaleService.insertResult(answerSheet);
    }
    @GetMapping(value = "/getAnswerType")
    public Map<String, Object> getAnswerType(){
        Map<String,List<AnswerType>> answerTypes = tscaleService.getAnswerType();
        return MyResult.getListResultMap(0, "success", 0, answerTypes);
    }
    @GetMapping(value = "/getForms")
    public Map<String, Object> getForms(@RequestParam String token){
        List<Form> forms = tscaleService.getForms(token);
        return MyResult.getListResultMap(0, "success", 0, forms);
    }
    @GetMapping(value = "/queryProgramsByPage")
    public Map<String, Object> queryProgramsByPage(@RequestParam Map<String, Object> params){
//        MyUtils.parsePageParams(params);
        int count = tscaleService.getProgramCount(params);  // 获得总数
        List<TProgram> TPrograms = tscaleService.searchProgramByPage(params);  // 分页查询
//        System.out.println("controller运行成功");
        return MyResult.getListResultMap(0, "success", count, TPrograms);
    }
    @PostMapping(value = "/insertTProgram")
    public int insertTProgram(@RequestBody TProgram tProgram){
//        System.out.println("接受数据：");
//        System.out.println("tProgram:" +tProgram);
//        System.out.println("userId:"+userId);
//        return 0;

        return tscaleService.insertTProgram(tProgram);
    }
    @DeleteMapping(value = "/deleteTPrograms")
    public Integer deleteTPrograms(@RequestBody List<TProgram> tPrograms){
        return tscaleService.deleteTPrograms(tPrograms);
    }
    @DeleteMapping(value = "/deleteTProgram")
    public Integer deleteTProgram(@RequestBody TProgram tProgram){
        return tscaleService.deleteTProgram(tProgram);
    }
    @PutMapping(value = "/updateTProgram")
    public Integer updateTProgram(@RequestBody TProgram tProgram){
        return tscaleService.updateTProgram(tProgram);
    }
    @GetMapping(value = "/getRyxx")
    public Map<String, Object> getRyxx(){
        List<Ryxx> ryxxes = tscaleService.getRyxx();
        return MyResult.getListResultMap(0, "success", 0, ryxxes);
    }
    @PostMapping(value = "/insertRyxx")
    public int insertRyxx(@RequestBody Ryxx ryxx){
        System.out.println("接受数据："+ryxx);
//        System.out.println("tProgram:" +tProgram);
//        System.out.println("userId:"+userId);
//        return 0;

        return tscaleService.insertRyxx(ryxx);
    }
    @GetMapping(value = "/selectRyxx")
    public Map<String, Object> selectRyxx(@RequestParam Map<String,Object> ryxx){
        List<Ryxx> ryxxes = tscaleService.selectRyxx(ryxx);
        return MyResult.getListResultMap(0, "success", 0, ryxxes);
    }
    @PutMapping(value = "/updateRyxx")
    public Integer updateRyxx(@RequestBody Ryxx ryxx){
        return tscaleService.updateRyxx(ryxx);
    }

    @PostMapping(value = "/insertPreProgram")
    public int insertPreProgram(@RequestBody Map<String,Object> param){
        System.out.println("接受数据："+param);
        return tscaleService.insertPreProgram(param);
    }
    @GetMapping(value = "/getItems")
    public Map<String, Object> getItems(@RequestParam String scaleId){
        List<Map<String,Object>> items = tscaleService.getItems(scaleId);
        return MyResult.getListResultMap(0, "success", 0, items);
    }
    @PostMapping(value = "/newInsertTProgram")
    public Map<String, Object> newInsertTProgram(@RequestBody TProgram tProgram){
//        System.out.println("接受数据：");
//        System.out.println("tProgram:" +tProgram);
//        System.out.println("userId:"+userId);
//        return 0;

        return tscaleService.newInsertTProgram(tProgram);
    }
    @PutMapping(value = "/TProgramStatus")
    public Integer TProgramStatus(@RequestBody Map<String,Object> param){
        return tscaleService.TProgramStatus(param);
    }
}

